package com.mutouren.modules.datapush.client;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import com.mutouren.modules.datapush.LocalManager;
import com.mutouren.modules.datapush.base.Constant;
import com.mutouren.modules.datapush.base.PushClient;
import com.mutouren.modules.datapush.local.HttpServerProxy;

/**
 * Demo: 混合模式，需要启动 mtr-datapush-server 服务 tomcat 或 netty
 * 
 * 注：含1个本地推送服务，2个远端推送服务
 * 
 */
public class PushClientTest_Remote2 {
	private final static String serverAlias1 = "pushserver-proxy1";
	private final static String serverUrl1 = "http://192.168.1.105:8080/mtr-datapush-server-web/api/send";
	private final static String serverAlias2 = "pushserver-proxy2";
	private final static String serverUrl2 = "http://192.168.1.106:9080/mtr-datapush-server-web/api/send";	
	
	@BeforeClass
	public static void before() {
		System.out.println("event: before");
		LocalManager.register(new HttpServerProxy(serverAlias1, serverUrl1));	
		LocalManager.register(new HttpServerProxy(serverAlias2, serverUrl2));
		
		LocalManager.open("e:/logs-data/push-data", true);
	}
	
	@AfterClass
	public static void after() {
		//LocalManager.close();
		System.out.println("event: after");		
	}
	
	@Test
	public void test_local() throws InterruptedException {

		// 以下两种写法等价
		PushClient dpClient = new SimplePushClient("http://localhost:8080/mtr-web-demo/api/demo/hello");
		//PushClient dpClient = new SimplePushClient("http://localhost:8080/mtr-web-demo/api/demo/hello", Constant.defaultMethodAlias);

		String dataPackage = "Hello, I am local server"; 
		dpClient.send(dataPackage);

		Thread.sleep(10000);
		System.out.println("event: test() is end");	
	}	

	@Test
	public void test_remote1() throws InterruptedException {
		String url = "http://localhost:8080/mtr-web-demo/api/demo/hello?helloword=ok";
		
		PushClient dpClient = new SimplePushClient(url, Constant.defaultMethodAlias, serverAlias1);
		
		for(int i = 0; i < 1; i ++ ) {
			String dataPackage = "Hello, I am local server: " + i; 
			dpClient.send(dataPackage);
		}	

		Thread.sleep(5000);		
		System.out.println("event: test() is end");	
	}
	
	@Test
	public void test_remote2() throws InterruptedException {
		String url = "http://localhost:8080/mtr-web-demo/api/demo/hello?helloword=ok";
		
		PushClient dpClient = new SimplePushClient(url, Constant.defaultMethodAlias, serverAlias2);
		
		for(int i = 0; i < 1; i ++ ) {
			String dataPackage = "Hello, I am local server: " + i; 
			dpClient.send(dataPackage);
		}	

		Thread.sleep(5000);
		
		System.out.println("event: test() is end");	
	}
	
	
}
